import {DetailModel} from "../../../../models/detail";
import {ActionModel} from "../../../../models/action";

const detailModel = new DetailModel()
const actionModel = new ActionModel()
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    posting: false,
    q: '',
    inputMarBot: false,
    isFollow:false,
    isCollect:false,
    isClock:false,
    placeId:'',
    placeInfo:{},
    commentList:[],
    userAvatarUrlList:[]
  },

  bindClickFollow: function (event) {
    let value = event.detail.isFollow
    let userId = this.data.placeInfo.user.userId
    return value?this._followUser(userId,value):this._cancelUser(userId,value)
  },

  //图片点击事件
  imgPreview:function(event){
    let src = event.currentTarget.dataset.src;//获取data-src
    let imgList = event.currentTarget.dataset.list;//获取data-list
    //图片预览
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: imgList // 需要预览的图片http链接列表
    })
  },

  handlerClickOnCollect: function (event) {
    let value = event.detail.isCollect
    let placeId = this.data.placeInfo.placeId
    this.setData({
      isCollect: value
    })
    return value?this._collectPlace(placeId,value):this._cancelPlace(placeId,value)
  },

  handlerOnClock: function (event) {
    let value = event.detail.isClock
    if (value) {
      app.showInfo('已打卡，无需重复打卡')
    } else {
      this._clockPlace(this.data.placeId)
    }
  },
  _clockPlace(placeId) {
    actionModel.clockPlace(placeId).then(res=>{
      app.showInfo('打卡成功',app.SUCCESS)
      this.setData({
        userAvatarUrlList:res.data,
        isClock:true
      })
    })
  },

  _commentPlace(placeId, content) {
    let _this = this
    actionModel.commentPlace(placeId,content).then(res=>{
      app.showInfo('评论成功',app.SUCCESS)
      _this.setData({
        commentList:res.data
      })
    })
  },

  // 评论输入框聚焦时，设置与底部的距离
  settingMbShow: function () {
    this.setData({
      inputMarBot: true
    })
  },
  //  评论输入框失去聚焦时，设置与底部的距离（默认状态）
  settingMbNoShow: function () {
    this.setData({
      inputMarBot: false
    })
  },
  openOrCloseTapComment: function () {
    console.log(this.data.posting)
    this.setData({
      posting:!this.data.posting
    })
  },

  watchPassWord: function (event) {
    this.setData({
      q:event.detail.value
    })
  },

  sendTap: function () {
    if (this.data.q.length == 0) {
      return
    }
    this._commentPlace(this.data.placeId,this.data.q)
    this.setData({
      posting:!this.data.posting,
      q:''
    })
  },

  onConfirm:function(event){
    const q = event.detail.value
    console.log(q)
    this.setData({
      q
    })
  },

  _collectPlace(placeId, value) {
    actionModel.collectPlace(placeId).then(res=>{
      this.setData({
        isCollect: value
      })
      app.showInfo('收藏成功！',app.SUCCESS)
    })
  },

  _cancelPlace(placeId, value) {
    actionModel.cancelPlace(placeId).then(res=>{
      this.setData({
        isCollect: value
      })
      app.showInfo('已取消收藏！',app.SUCCESS)
    })
  },

  _followUser(userId,value) {
    actionModel.followUser(userId).then(res=>{
      this.setData({
        isFollow: value
      })
      app.showInfo('已关注！',app.SUCCESS)
    })
  },

  _cancelUser(userId,value) {
    actionModel.cancelUser(userId).then(res=>{
      this.setData({
        isFollow: value
      })
      app.showInfo('已取消关注！',app.SUCCESS)
    })
  },

  _init(placeId) {
    detailModel.getPlaceDetail(placeId).then(res=>{
      this.setData({
        placeInfo:res.data,
        userAvatarUrlList:res.data.userAvatarUrlList,
        commentList:res.data.items,
        isFollow:res.data.user.follow,
        isCollect:res.data.collect,
        isClock:res.data.clock,
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      placeId:options.placeId
    })
    this._init(options.placeId)
    let isIphoneX = app.globalData.isIphoneX;
    this.setData({
      isIphoneX: isIphoneX
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      desc: '推荐场地:'+this.data.placeInfo.placeName+' - PHOS',
      path: '/pages/index/details/site/index?id='+this.data.placeInfo.placeId,
      imageUrl:this.data.placeInfo.imageList[0]
    }
  }
})
